import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/domain/application/organization/AttendanceDayStatistics.dart';
import 'package:bxultimate/domain/application/organization/AttendanceYearStatistics.dart';
import 'package:bxultimate/domain/application/organization/PersonAttendanceClickingDto.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_calendar.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/mk_card_container.dart';
import 'package:bxultimate/widgets/mk_list_cell_item.dart';
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class AttendanceAnalysis extends StatefulWidget {
  final String orgId;

  const AttendanceAnalysis({super.key, required this.orgId});

  @override
  State<AttendanceAnalysis> createState() => _AttendanceAnalysisState();
}

class _AttendanceAnalysisState extends State<AttendanceAnalysis> {
  List<TDStepsItemData> vBasicStepsListData = [
    TDStepsItemData(title: 'Filish', content: 'Customize content'),
    TDStepsItemData(title: 'Process', content: 'Customize content'),
    TDStepsItemData(title: 'Default', content: 'Customize content'),
    TDStepsItemData(title: 'Default', content: 'Customize content'),
  ];
  List<PersonAttendanceClickingDto> dtoList = [];
  List<String> currentClockingList = [];

  DateTime selectDate = DateTime.now();
  List<num> checkDate = [];
  List<AttendanceDayStatistics> dayAnalysis = [];
  late PersonAttendanceClickingDto currentClocking =
      PersonAttendanceClickingDto();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getData();
  }

  getData() async {
    dtoList =
        await OrganizationRequest.getPersonAttendanceClockingList(widget.orgId);
    if (dtoList.length > 0) {
      currentClocking = dtoList[0];
      selectDate = DateTime.now();
      getCheckDate();
    }
    currentClockingList = dtoList.map((e) => e.title!).toList();
    // 获取月的考勤列表
    setState(() {});
  }

  getCheckDate() async {
    // 获取当前年月
    var yearMonth = selectDate.year.toString() +
        '-' +
        selectDate.month.toString().padLeft(2, '0');
    List<AttendanceYearStatistics> yearAnalysis =
        await OrganizationRequest.getPersonStatisticsByMonth(
            currentClocking.id, yearMonth);

    var day = selectDate.year.toString() +
        '-' +
        selectDate.month.toString().padLeft(2, '0') +
        '-' +
        selectDate.day.toString().padLeft(2, '0');
    dayAnalysis = await OrganizationRequest.getPersonStatisticsByDay(
        currentClocking.id, day);
    checkDate = yearAnalysis.map((e) => int.parse(e.date!)).toList();
    setState(() {});
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.only(
          left: AppTheme.iDefaultPadding, right: AppTheme.iDefaultPadding),
      child: Column(children: [
        _buildNav(),
        Gaps.vGap12,
        AppCalendar(
          checkDate: checkDate,
          manuallySelectedDate: DateTime.now(),
          onDateSelected: (date) {
            selectDate = date;
            getCheckDate();
          },
        ),
        Gaps.vGap12,
        AppTitle(title: '签到${dayAnalysis.length}次'),
        Gaps.vGap8,
        ...dayAnalysis.map((e) => TDCell(
          image: AssetImage(Assets.organizationAttendance,),
          arrow: false,
          title: '${e.clockingTime}',
          description: '${e.address}',
        )),
      ]),
    );
  }

  _buildNav() {
    return MkCardContainer(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding / 2),
      margin: EdgeInsets.all(0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          MkListCellItem(
            value: '考勤标题',
            ext: Text(
                currentClocking.id == null ? '暂无考勤' : currentClocking.title!),
            press: selectAttendance,
          ),
        ],
      ),
    );
  }

  selectAttendance() async {
    TDPicker.showMultiPicker(context, title: '选择考勤', onConfirm: (selected) {
      setState(() {
        currentClocking = dtoList[selected[0]];
        getCheckDate();
      });
      Navigator.of(context).pop();
    }, data: [currentClockingList]);
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: _buildBody(),
    );
  }
}
